<?php

class Login extends CT_Controller {

    function __construct() {
        parent::__construct();
        
        $this->load->model('member_model');
        $this->load->model('teacher_model');
        $this->load->library('CT_auth_member_teacher');
    }

    public function index(){
        $this->load->library('form_validation');
        
        if( $this->input->is_ajax_request()){ 
            $mobile = $this->input->post('mobile',TRUE);
            $code = $this->input->post('sms_code');
            
            $key = 'login_'.$mobile;
            $sess_code = $this->session->tempdata($key);
           
            if (!$code || !$sess_code || strtolower($code) != strtolower($sess_code)){
                $this->show_json_data(NULL,'E_400','验证码错误');
                exit;
            }
               
            if(!check_mobile($mobile)){
                $this->show_json_data(NULL,'E_1001');
                exit;            
            }
            
            $teacher_info = $this->teacher_model->find(array('mobile' => $mobile, 'status'=>1));
            $member_info = $this->member_model->find(array('login_mobile' => $mobile, 'status'=>1));
            
            if ($teacher_info) {
                $data = array('wx_open_id'=>$this->_open_id,
                            'is_login'=>1,
                            'last_login_date'=>date('Y-m-d H:i:s'),
                            'update_date'=>date('Y-m-d H:i:s')
                );
                $this->teacher_model->update($data,array('id'=>$teacher_info['id']));
                
                $sess_data = array('id'=>$teacher_info['id'],
                                    'nursery_id'=>$teacher_info['nursery_id'],
                                    'is_teacher'=>1
                );
                
            }elseif($member_info){
                $data = array('wx_open_id'=>$this->_open_id,
                            'is_login'=>1,
                            'last_login_date'=>date('Y-m-d H:i:s'),
                            'update_date'=>date('Y-m-d H:i:s')
                );
                $this->member_model->update($data,array('id'=>$member_info['id']));
                
                $sess_data = array('id'=>$member_info['id'],
                    'nursery_id'=>$member_info['nursery_id'],
                    'is_teacher'=>0
                );
                
            }else{
                $member_info = $this->member_model->find(array('wx_open_id' => $this->_open_id, 'status'=>1));
                $data = array('login_mobile'=>$mobile,
                            'is_login'=>1,
                            'last_login_date'=>date('Y-m-d H:i:s'),
                            'update_date'=>date('Y-m-d H:i:s')
                );
                $this->member_model->update($data,array('id'=>$member_info['id']));
                
                $sess_data = array('id'=>$member_info['id'],
                    'nursery_id'=>$member_info['nursery_id'],
                    'is_teacher'=>0
                );
            }
            
            $this->ct_auth_member_teacher->set_user_login($sess_data);
            
            $this->show_json_data();
            exit;
            
        }
        
        $return_url = $this->input->get('url', TRUE) ? urldecode($this->input->get('url', TRUE)) : '';
        $data['return_url'] = $return_url;
        
        $this->ct_template->write('_title', '登陆');
        $this->ct_template->write_view('content', 'login/index', $data, FALSE);
        $this->ct_template->render();
    }
    
    public function logout()
    {
        $uid = intval($this->input->get('uid', TRUE));
        
        if($uid){
            $data = array('is_login'=>0, 'update_date'=>date('Y-m-d H:i:s'));
            if($this->_is_teacher){
                $this->teacher_model->update($data,array('id'=>$uid));
            }else{
                $this->member_model->update($data,array('id'=>$uid));
            }
            
//             $this->ct_auth_member_teacher->set_user_logout();
            unset($_SESSION['uid']);
            unset($_SESSION['token']);
        }
        
        redirect('/login');
    }


}
